using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
using Matrix.Notification.Domain.Shared.Notifications;

namespace Matrix.Notification.Domain.Notifications
{
    /// <summary>
    /// 通知模板仓储接口
    /// </summary>
    public interface INotificationTemplateRepository : IRepository<NotificationTemplate, Guid>
    {
        /// <summary>
        /// 根据名称获取模板
        /// </summary>
        Task<NotificationTemplate?> GetByNameAsync(
            string name,
            Guid? tenantId = null,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 根据编码获取模板
        /// </summary>
        Task<NotificationTemplate?> GetByCodeAsync(
            string templateCode,
            Guid? tenantId = null,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 根据类型获取模板列表
        /// </summary>
        Task<List<NotificationTemplate>> GetByTypeAsync(
            TemplateType type,
            Guid? tenantId = null,
            bool? isEnabled = null,
            int maxResultCount = 10,
            int skipCount = 0,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 根据通知类型获取模板列表
        /// </summary>
        Task<List<NotificationTemplate>> GetByNotificationTypeAsync(
            NotificationType notificationType,
            Guid? tenantId = null,
            bool? isEnabled = null,
            int maxResultCount = 10,
            int skipCount = 0,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 获取启用的模板列表
        /// </summary>
        Task<List<NotificationTemplate>> GetEnabledTemplatesAsync(
            Guid? tenantId = null,
            TemplateType? type = null,
            NotificationType? notificationType = null,
            int maxResultCount = 10,
            int skipCount = 0,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 获取系统模板列表
        /// </summary>
        Task<List<NotificationTemplate>> GetSystemTemplatesAsync(
            TemplateType? type = null,
            NotificationType? notificationType = null,
            int maxResultCount = 10,
            int skipCount = 0,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 获取用户自定义模板列表
        /// </summary>
        Task<List<NotificationTemplate>> GetCustomTemplatesAsync(
            Guid? tenantId,
            TemplateType? type = null,
            NotificationType? notificationType = null,
            int maxResultCount = 10,
            int skipCount = 0,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 根据分类获取模板列表
        /// </summary>
        Task<List<NotificationTemplate>> GetByCategoryAsync(
            string category,
            Guid? tenantId = null,
            bool? isEnabled = null,
            int maxResultCount = 10,
            int skipCount = 0,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 根据标签搜索模板
        /// </summary>
        Task<List<NotificationTemplate>> SearchByTagsAsync(
            string[] tags,
            Guid? tenantId = null,
            bool? isEnabled = null,
            int maxResultCount = 10,
            int skipCount = 0,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 根据关键词搜索模板
        /// </summary>
        Task<List<NotificationTemplate>> SearchAsync(
            string keyword,
            Guid? tenantId = null,
            TemplateType? type = null,
            NotificationType? notificationType = null,
            bool? isEnabled = null,
            int maxResultCount = 10,
            int skipCount = 0,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 获取支持指定渠道的模板
        /// </summary>
        Task<List<NotificationTemplate>> GetBySupportedChannelAsync(
            NotificationChannel channel,
            Guid? tenantId = null,
            bool? isEnabled = null,
            int maxResultCount = 10,
            int skipCount = 0,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 获取热门模板
        /// </summary>
        Task<List<NotificationTemplate>> GetPopularTemplatesAsync(
            int maxResultCount = 10,
            Guid? tenantId = null,
            DateTime? since = null,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 获取最近使用的模板
        /// </summary>
        Task<List<NotificationTemplate>> GetRecentlyUsedTemplatesAsync(
            int maxResultCount = 10,
            Guid? tenantId = null,
            DateTime? since = null,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 获取高评分模板
        /// </summary>
        Task<List<NotificationTemplate>> GetTopRatedTemplatesAsync(
            int maxResultCount = 10,
            Guid? tenantId = null,
            double minRating = 4.0,
            int minRatingCount = 5,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 根据审核状态获取模板
        /// </summary>
        Task<List<NotificationTemplate>> GetByApprovalStatusAsync(
            TemplateApprovalStatus approvalStatus,
            Guid? tenantId = null,
            int maxResultCount = 10,
            int skipCount = 0,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 获取待审核的模板
        /// </summary>
        Task<List<NotificationTemplate>> GetPendingApprovalTemplatesAsync(
            Guid? tenantId = null,
            int maxResultCount = 10,
            int skipCount = 0,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 获取子模板列表
        /// </summary>
        Task<List<NotificationTemplate>> GetChildTemplatesAsync(
            Guid parentTemplateId,
            Guid? tenantId = null,
            bool? isEnabled = null,
            int maxResultCount = 10,
            int skipCount = 0,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 获取模板的子模板数量
        /// </summary>
        Task<int> GetChildTemplateCountAsync(
            Guid parentTemplateId,
            Guid? tenantId = null,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 检查模板名称是否存在
        /// </summary>
        Task<bool> NameExistsAsync(
            string name,
            Guid? excludeId = null,
            Guid? tenantId = null,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 检查模板编码是否存在
        /// </summary>
        Task<bool> CodeExistsAsync(
            string templateCode,
            Guid? excludeId = null,
            Guid? tenantId = null,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 获取模板统计信息
        /// </summary>
        Task<TemplateStatistics> GetStatisticsAsync(
            Guid? tenantId = null,
            CancellationToken cancellationToken = default);
    }

    /// <summary>
    /// 模板统计信息
    /// </summary>
    public class TemplateStatistics
    {
        /// <summary>
        /// 总模板数
        /// </summary>
        public int TotalCount { get; set; }

        /// <summary>
        /// 启用模板数
        /// </summary>
        public int EnabledCount { get; set; }

        /// <summary>
        /// 禁用模板数
        /// </summary>
        public int DisabledCount { get; set; }

        /// <summary>
        /// 系统模板数
        /// </summary>
        public int SystemCount { get; set; }

        /// <summary>
        /// 自定义模板数
        /// </summary>
        public int CustomCount { get; set; }

        /// <summary>
        /// 按类型统计
        /// </summary>
        public Dictionary<TemplateType, int> CountByType { get; set; } = new();

        /// <summary>
        /// 按通知类型统计
        /// </summary>
        public Dictionary<NotificationType, int> CountByNotificationType { get; set; } = new();

        /// <summary>
        /// 按审核状态统计
        /// </summary>
        public Dictionary<TemplateApprovalStatus, int> CountByApprovalStatus { get; set; } = new();

        /// <summary>
        /// 平均使用次数
        /// </summary>
        public double AverageUsageCount { get; set; }

        /// <summary>
        /// 总使用次数
        /// </summary>
        public int TotalUsageCount { get; set; }

        /// <summary>
        /// 平均评分
        /// </summary>
        public double AverageRating { get; set; }

        /// <summary>
        /// 评分模板数
        /// </summary>
        public int RatedTemplateCount { get; set; }
    }
}